﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace WebHttpApi.WebHttpApi.Core
{
    /// <summary>
    /// TyNB 的摘要说明：NB水表指令类
    /// </summary>
    public class TyNB
    {       
        public static string str_MeterId = "";                 //表地址逆序排列
        public static string str_Comm = "";                    //命令串
        public static string str_CommPam = "";                 //参数串

        public static string str_zxjg = "0";                   //执行结果  -  0：未执行 | 1：已执行
        public static string str_xyjx = "";                    //协议解析  -  什么命令（日志）
        public static string str_rzlx = "存储";                //日志类型  -  存储：命令写数据库（日志）
        public static string str_xymc = "";                    //协议名称  -  命令类型名称： 控阀、休眠等（日志）
        public static string str_oper = "WebHttpApi";          //操作员    -  指令、日志

        public static string str_Result = "";
        /// <summary>
        /// NB接口调用
        /// </summary>
        /// <param name="MeterType">仪表类型</param>
        /// <param name="MeterId">仪表地址</param>
        /// <param name="Type">状态</param>
        /// <param name="CommPam"></param>
        /// <returns></returns>
        public static string Nb_Comm(string MeterType, string MeterId, string Type, string CommPam)
        {
            str_xyjx = "";
            str_xymc = "";

            if (MeterId != "" && Type != "")
            {
                
                try
                {
                    str_MeterId = MeterId.Substring(12, 2) +
                                  MeterId.Substring(10, 2) +
                                  MeterId.Substring(8, 2) +
                                  MeterId.Substring(6, 2) +
                                  MeterId.Substring(4, 2) +
                                  MeterId.Substring(2, 2) +
                                  MeterId.Substring(0, 2);

                    switch (Type)
                    {
                        //控阀指令 
                        //Type：43 | 67
                        //CommPam：55、99、66 | 85、153、102
                        case "67":
                            {
                                if (CommPam=="85")
                                {
                                    str_CommPam = "55";
                                    str_xyjx = "开阀";
                                }
                                else if (CommPam == "153")
                                {
                                    str_CommPam = "99";
                                    str_xyjx = "关阀";
                                }
                                else if (CommPam == "102")
                                {
                                    str_CommPam = "66";
                                    str_xyjx = "阀门测试";
                                }
                                else
                                {
                                    str_Result = "参数错误";
                                    break;
                                }
                                str_xymc = "控阀";
                            
                                if (MeterType == "1" ||
                                    MeterType == "3" ||
                                    MeterType == "4" ||
                                    MeterType == "6" ||
                                    MeterType == "7" ||
                                    MeterType == "9" ||
                                    MeterType == "10" ||
                                    MeterType == "13" ||
                                    MeterType == "14" ||
                                    MeterType == "15")
                                {
                                    str_Comm = str_MeterId + "4310000001" + str_CommPam;
                                    str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                    str_Comm = "1B06" + str_Comm;
                                    str_Comm = str_Comm.ToUpper();
                                    str_Result = NB_sql(MeterId, Type);
                                }
                                /*增加了长沙协议控制指令结束*/
                                else if (MeterType == "17"||MeterType=="16"||MeterType=="18")
                                {
                                    str_Comm = "6810"+str_MeterId + "0404A01700" + str_CommPam;
                                    str_Comm = str_Comm + WebSdkCore.CS(str_Comm)+"16";                               
                                    str_Comm = str_Comm.ToUpper();
                                    str_Result = NB_sql(MeterId, Type);
                                }
                                /*增加了外部协议控制指令结束*/
                                else if (MeterType == "244")
                                {

                                    str_Comm = str_CommPam;
                                    str_Result = NB_sql(MeterId, Type);
                                }
                                else
                                {
                                    str_Result = "不支持";
                                }
                              

                                break;
                            }

                        //参数设置
                        //Type：44 | 68
                        //CommPam：00 00  (1、用户状态；2、脉冲当量；)
                        case "68":
                            {
                                str_xyjx = "参数设置[" + CommPam + "]";
                                str_xymc = "参数设置";

                                try
                                {
                                    string[] str = CommPam.Split(':');

                                    string str_yhzt = str[0];
                                    str_yhzt = "00" + str_yhzt;
                                    str_yhzt = str_yhzt.Substring(str_yhzt.Length - 2);

                                    string str_mcdl = str[1];
                                    str_mcdl = "00" + str_mcdl;
                                    str_mcdl = str_mcdl.Substring(str_mcdl.Length - 2);

                                    str_CommPam = str_yhzt + str_mcdl;
                                }
                                catch
                                {
                                    str_Result = "参数错误";
                                    break;
                                }

                                str_Comm = str_MeterId + "4410000002" + str_CommPam;
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = NB_sql(MeterId, Type);

                                break;
                            }

                        //底数设置
                        //Type：45 | 69
                        //CommPam：000000 （3字节HEX：低在前，脉冲数,根据脉冲当量计算）
                        case "69":
                            {
                                str_xyjx = "底数设置[" + CommPam + "]";
                                str_xymc = "底数设置";

                                string Meter_mcdl;
                                int str_mcdl;
                              
                               
                            

                                try
                                {


                                    string sql = "select ybdz,mcdl from rp_meter_jk where ybdz='" +
                                                    MeterId + "'";

                                    System.Data.DataTable Meter_Data = WebSdkCore.select(sql);
                                    if (MeterType == "17" || MeterType == "16")
                                    {
                                        //string sql = "select ybdz,mcdl from rp_meter_jk where ybdz='" +
                                        //               MeterId + "'";

                                        //System.Data.DataTable Meter_Data = WebSdkCore.select(sql);

                                        if (Meter_Data.Rows.Count > 0)
                                        {
                                            Meter_mcdl = Meter_Data.Rows[0]["mcdl"].ToString();
                                            str_mcdl = 0;
                                            switch (Meter_mcdl)
                                            {
                                                case "00":
                                                    str_mcdl = 1;
                                                    break;
                                                case "01":
                                                    str_mcdl = 10;
                                                    break;
                                                case "02":
                                                    str_mcdl = 100;
                                                    break;
                                                case "03":
                                                    str_mcdl = 1000;
                                                    break;
                                                case "04":
                                                    str_mcdl = 10000;
                                                    break;
                                                case "05":
                                                    str_mcdl = 100000;
                                                    break;
                                            }


                                            str_Comm = "6810" + str_MeterId + "1608A01600" + WebSdkCore.Reverse_order((double.Parse(CommPam.Trim()) * str_mcdl).ToString().PadLeft(8, '0')) + "2C";
                                            str_Comm = str_Comm + WebSdkCore.CS(str_Comm)+"16";
                                            str_Comm = str_Comm.ToUpper();
                                            str_Result = NB_sql(MeterId, Type);
                                            break;
                                        }
                                    }

                                    if (Meter_Data.Rows.Count > 0)
                                    {
                                        Meter_mcdl = Meter_Data.Rows[0]["mcdl"].ToString();
                                        Meter_mcdl = Regex.Replace(Meter_mcdl, @"[^0-9,.]+", "");

                                        if (Meter_mcdl == "0.1")
                                        {
                                            str_mcdl = 1;
                                        }
                                        else if (Meter_mcdl == "0.01")
                                        {
                                            str_mcdl = 10;
                                        }
                                        else if (Meter_mcdl == "0.001")
                                        {
                                            str_mcdl = 100;
                                        }
                                        else
                                        {
                                            str_Result = "参数错误";
                                            break;
                                        }

                                        str_CommPam = Convert.ToInt64(Convert.ToDouble(CommPam) * str_mcdl).ToString("x");
                                        str_CommPam = "000000" + str_CommPam;
                                        str_CommPam = str_CommPam.Substring(str_CommPam.Length - 6);

                                        str_CommPam = str_CommPam.Substring(4, 2) +
                                                      str_CommPam.Substring(2, 2) +
                                                      str_CommPam.Substring(0, 2);
                                    }
                                  
                                    else
                                    {
                                        str_Result = "仪表不存在";
                                        break;
                                    }
                                }
                                catch
                                {
                                    str_Result = "参数错误";
                                    break;
                                }

                                str_Comm = str_MeterId + "4510000003" + str_CommPam;
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = NB_sql(MeterId, Type);

                                break;
                            }

                        //参数读取
                        //Type：46 | 70
                        case "70":
                            {
                                str_xyjx = "参数读取";
                                str_xymc = "参数读取";

                                str_Comm = str_MeterId + "4610000000";
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                //该指令获取的信息在上报的实时数据中包含，不开放此指令
                                //str_Result = NB_sql(MeterId, Type);

                                break;
                            }

                        //读程序版本
                        //Type：47 | 71
                        case "71":
                            {
                                str_xyjx = "读程序版本";
                                str_xymc = "读程序版本";

                                str_Comm = str_MeterId + "4710000000";
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                //该指令获取的信息在上报的实时数据中包含，不开放此指令
                                //str_Result = NB_sql(MeterId, Type);

                                break;
                            }

                        //读表地址
                        //Type：48 | 72
                        case "72":
                            {
                                str_xyjx = "读表地址";
                                str_xymc = "读表地址";

                                str_Comm = str_MeterId + "4810000000";
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                //该指令获取的信息在上报的实时数据中包含，不开放此指令
                                //str_Result = NB_sql(MeterId, Type);

                                break;
                            }

                        //设表地址
                        //Type：49 | 73
                        //CommPam：14为新仪表地址号
                        case "73":
                            {
                                str_xyjx = "设表地址[" + CommPam + "]";
                                str_xymc = "设表地址";

                                if (CommPam.Length == 14)
                                {
                                    try
                                    {
                                        str_CommPam = CommPam.Substring(12, 2) +
                                                      CommPam.Substring(10, 2) +
                                                      CommPam.Substring(8, 2) +
                                                      CommPam.Substring(6, 2) +
                                                      CommPam.Substring(4, 2) +
                                                      CommPam.Substring(2, 2) +
                                                      CommPam.Substring(0, 2);
                                    }
                                    catch
                                    {
                                        str_Result = "参数错误";
                                        break;
                                    }

                                    str_Comm = str_MeterId + "4910000007" + str_CommPam;
                                    str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                    str_Comm = "1B06" + str_Comm;
                                    str_Comm = str_Comm.ToUpper();

                                    str_Result = NB_sql(MeterId, Type);
                                }
                                else
                                {
                                    str_Result = "参数错误";
                                }
                                break;
                            }

                        //设置IP端口
                        //Type：4A | 74
                        //CommPam：IP:端口
                        case "74":
                            {
                                str_xyjx = "设置IP端口[" + CommPam + "]";
                                str_xymc = "设置IP端口";

                                if (MeterType == "2" ||
                                    MeterType == "3" ||
                                    MeterType == "4")
                                {
                                    try
                                    {
                                        string[] str = CommPam.Split(':');

                                        string str_ip = str[0];
                                        string[] ip = str_ip.Split('.');

                                        string str_ip01 = "00" + Convert.ToInt64(ip[0]).ToString("x");
                                        str_ip01 = str_ip01.Substring(str_ip01.Length - 2);
                                        string str_ip02 = "00" + Convert.ToInt64(ip[1]).ToString("x");
                                        str_ip02 = str_ip02.Substring(str_ip02.Length - 2);
                                        string str_ip03 = "00" + Convert.ToInt64(ip[2]).ToString("x");
                                        str_ip03 = str_ip03.Substring(str_ip03.Length - 2);
                                        string str_ip04 = "00" + Convert.ToInt64(ip[3]).ToString("x");
                                        str_ip04 = str_ip04.Substring(str_ip04.Length - 2);

                                        str_ip = str_ip01 + str_ip02 + str_ip03 + str_ip04;

                                        string str_dk = str[1];
                                        str_dk = Convert.ToInt64(str_dk).ToString("x");
                                        str_dk = "0000" + str_dk;
                                        str_dk = str_dk.Substring(str_dk.Length - 4);

                                        str_CommPam = str_ip + str_dk;
                                    }
                                    catch
                                    {
                                        str_Result = "参数错误";
                                        break;
                                    }
                                }
                                else
                                {
                                    str_Result = "不支持";
                                    break;
                                }

                                str_Comm = str_MeterId + "4A10000006" + str_CommPam;
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = NB_sql(MeterId, Type);

                                break;
                            }

                        //读取IP端口
                        //Type：4B | 75
                        case "75":
                            {
                                str_xyjx = "读取IP端口";
                                str_xymc = "读取IP端口";
                            
                                if (MeterType == "2" ||
                                    MeterType == "3" ||
                                    MeterType == "4")
                                {
                                    str_Comm = str_MeterId + "4B10000000";
                                    str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                    str_Comm = "1B06" + str_Comm;
                                    str_Comm = str_Comm.ToUpper();
                                }
                                else
                                {
                                    str_Result = "不支持";
                                    break;
                                }

                                str_Result = NB_sql(MeterId, Type);
                            
                                break;
                            }

                        //设表时间:指令下发写当前时间
                        //Type：4C | 76
                        case "76":
                            {
                                str_xyjx = "设表时间";
                                str_xymc = "设表时间";

                                try
                                {
                                    string str_year = DateTime.Now.ToString("yyyy");
                                    str_year = "0000" + str_year;
                                    str_year = str_year.Substring(str_year.Length - 4);

                                    string str_month = DateTime.Now.ToString("MM");
                                    str_month = "00" + str_month;
                                    str_month = str_month.Substring(str_month.Length - 2);

                                    string str_day = DateTime.Now.ToString("dd");
                                    str_day = "00" + str_day;
                                    str_day = str_day.Substring(str_day.Length - 2);

                                    string str_hour = DateTime.Now.ToString("HH");
                                    str_hour = "00" + str_hour;
                                    str_hour = str_hour.Substring(str_hour.Length - 2);

                                    string str_minute = DateTime.Now.ToString("mm");
                                    str_minute = "00" + str_minute;
                                    str_minute = str_minute.Substring(str_minute.Length - 2);

                                    string str_second = DateTime.Now.ToString("ss");
                                    str_second = "00" + str_second;
                                    str_second = str_second.Substring(str_second.Length - 2);

                                    str_CommPam = str_year + str_month + str_day + str_hour + str_minute + str_second;
                                }
                                catch
                                {
                                    str_Result = "写指令失败";
                                    break;
                                }

                                str_Comm = str_MeterId + "4C10000007" + str_CommPam;
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = NB_sql(MeterId, Type);
                                break;
                            }

                        //故障清除
                        //Type：4D | 77
                        case "77":
                            {
                                str_xyjx = "故障清除";
                                str_xymc = "故障清除";

                                str_Comm = str_MeterId + "4D1000000111";
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                //不开放此指令
                                //str_Result = NB_sql(MeterId, Type);

                                break;
                            }

                        //设上报周期
                        //Type：4E | 78
                        //CommPam：上线间隔（10分钟）:时（0-23）:分（0-59）
                        case "78":
                            {
                                str_xyjx = "设上报周期[" + CommPam + "]";
                                str_xymc = "设上报周期";

                                try
                                {
                                    string[] str = CommPam.Split(':');

                                    string str_sbjg = str[0];
                                    if (0 <= Convert.ToInt64(str_sbjg) && Convert.ToInt64(str_sbjg) <= 144)
                                    {
                                        str_sbjg = "00" + Convert.ToInt64(str_sbjg).ToString("x");
                                        str_sbjg = str_sbjg.Substring(str_sbjg.Length - 2);
                                    }
                                    else
                                    {
                                        str_Result = "参数错误";
                                        break;
                                    }

                                    string str_hour = "00";
                                    string str_minute = "00";

                                    if (str_sbjg == "00")
                                    {
                                        str_hour = str[1];
                                        if (0 <= Convert.ToInt64(str_hour) && Convert.ToInt64(str_hour) <= 23)
                                        {
                                            str_hour = "00" + str_hour;
                                            str_hour = str_hour.Substring(str_hour.Length - 2);
                                        }
                                        else
                                        {
                                            str_Result = "参数错误";
                                            break;
                                        }

                                        str_minute = str[2];
                                        if (0 <= Convert.ToInt64(str_minute) && Convert.ToInt64(str_minute) <= 59)
                                        {
                                            str_minute = "00" + str_minute;
                                            str_minute = str_minute.Substring(str_minute.Length - 2);
                                        }
                                        else
                                        {
                                            str_Result = "参数错误:上线间隔:0-144(10分钟),上限时:0-23(时),上线分:0-59(分)";
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        str_hour = "00";
                                        str_minute = "00";
                                    }

                                    str_CommPam = str_sbjg + str_hour + str_minute;
                                }
                                catch
                                {
                                    str_Result = "参数错误";
                                    break;
                                }

                                str_Comm = str_MeterId + "4E10000003" + str_CommPam;
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = NB_sql(MeterId, Type);
                            
                                break;
                            }

                        //附着设置
                        //Type：58 | 88
                        //CommPam：附着时间上限（秒）:通讯时间上限（秒）
                        case "88":
                            {
                                str_xyjx = "附着设置[" + CommPam + "]";
                                str_xymc = "附着设置";

                                try
                                {
                                    string[] str = CommPam.Split(':');

                                    string str_fzsj = str[0];
                                    if (30 <= Convert.ToInt64(str_fzsj) && Convert.ToInt64(str_fzsj) <= 70)
                                    {
                                        str_fzsj = "00" + Convert.ToInt64(str_fzsj).ToString("x");
                                        str_fzsj = str_fzsj.Substring(str_fzsj.Length - 2);
                                    }
                                    else
                                    {
                                        str_Result = "参数错误";
                                        break;
                                    }

                                    string str_txsj = str[1];
                                    if (20 <= Convert.ToInt64(str_txsj) && Convert.ToInt64(str_txsj) <= 40)
                                    {
                                        str_txsj = "00" + Convert.ToInt64(str_txsj).ToString("x");
                                        str_txsj = str_txsj.Substring(str_txsj.Length - 2);
                                    }
                                    else
                                    {
                                        str_Result = "参数错误:附着时限30-70秒,通讯时限20-40秒";
                                        break;
                                    }

                                    str_CommPam = str_fzsj + str_txsj;
                                }
                                catch
                                {
                                    str_Result = "参数错误";
                                    break;
                                }

                                str_Comm = str_MeterId + "5810000002" + str_CommPam;
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = NB_sql(MeterId, Type);

                                break;
                            }

                        //附着读取
                        //Type：59 | 89
                        case "89":
                            {
                                str_xyjx = "附着读取";
                                str_xymc = "附着读取";

                                str_Comm = str_MeterId + "5910000000";
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = NB_sql(MeterId, Type);

                                break;
                            }

                        //清除复位
                        //Type：5B | 91
                        case "91":
                            {
                                str_xyjx = "清除复位";
                                str_xymc = "清除复位";

                                str_Comm = str_MeterId + "5B10000000";
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = NB_sql(MeterId, Type);

                                break;
                            }

                        //开关触摸上线
                        //Type：5C | 92
                        //CommPam：⑴、11 - 开启；⑵、22 - 关闭。
                        case "92":
                            {
                                str_xymc = "开关触摸上线";

                                if (CommPam == "11")
                                {
                                    str_xyjx = "开关触摸上线[开启]";

                                    str_CommPam = "00" + CommPam;
                                    str_CommPam = str_CommPam.Substring(str_CommPam.Length - 2);
                                }
                                else if (CommPam == "22")
                                {
                                    str_xyjx = "开关触摸上线[关闭]";

                                    str_CommPam = "00" + CommPam;
                                    str_CommPam = str_CommPam.Substring(str_CommPam.Length - 2);
                                }
                                else
                                {
                                    str_Result = "参数错误";
                                    break;
                                }

                                str_Comm = str_MeterId + "5C10000001" + str_CommPam;
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = NB_sql(MeterId, Type);

                                break;
                            }

                        //蓝牙NB
                        //充值指令
                        //Type：61 | 97
                        //CommPam：充值量(m³):区域代码
                        case "97":
                            {
                                str_xyjx = "蓝牙|充值[" + CommPam + "]";
                                str_xymc = "蓝牙|充值";

                                if (MeterType == "4" ||
                                    MeterType == "7" ||
                                    MeterType == "10"||
                                     MeterType == "13"||
                                      MeterType == "14"||
                                       MeterType == "15"
                                    )
                                {
                                    try
                                    {
                                        string[] str = CommPam.Split(':');

                                        string str_czsl = str[0];
                                        str_czsl = (Convert.ToDouble(str_czsl) * 10).ToString();
                                        if (0 <= Convert.ToInt64(str_czsl) && Convert.ToInt64(str_czsl) <= 4294967295)
                                        {
                                            str_czsl = "00000000" + Convert.ToInt64(str_czsl).ToString("x");
                                            str_czsl = str_czsl.Substring(str_czsl.Length - 8);
                                        }
                                        else
                                        {
                                            str_Result = "参数错误";
                                            break;
                                        }

                                        string str_qydm = str[1];
                                        if (0 <= Convert.ToInt64(str_qydm) && Convert.ToInt64(str_qydm) <= 65535)
                                        {
                                            str_qydm = "0000" + Convert.ToInt64(str_qydm).ToString("x");
                                            str_qydm = str_qydm.Substring(str_qydm.Length - 4);
                                        }
                                        else
                                        {
                                            str_Result = "参数错误";
                                            break;
                                        }

                                        str_CommPam = str_czsl + str_qydm;
                                    }
                                    catch
                                    {
                                        str_Result = "参数错误";
                                        break;
                                    }
                                }
                                else
                                {
                                    str_Result = "不支持";
                                    break;
                                }

                                str_Comm = str_MeterId + "6110000006" + str_CommPam;
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = NB_sql(MeterId, Type);

                                break;
                            }

                        //蓝牙NB
                        //未开户充值指令
                        //Type：62 | 98
                        //CommPam：未开户充值量(m³):区域代码
                        case "98":
                            {
                                str_xyjx = "蓝牙|未开户充值[" + CommPam + "]";
                                str_xymc = "蓝牙|未开户充值";

                                if (MeterType == "4" ||
                                    MeterType == "7" ||
                                    MeterType == "10")
                                {
                                    try
                                    {
                                        string[] str = CommPam.Split(':');

                                        string str_czsl = str[0];
                                        str_czsl = (Convert.ToDouble(str_czsl) * 10).ToString();
                                        if (0 <= Convert.ToInt64(str_czsl) && Convert.ToInt64(str_czsl) <= 4294967295)
                                        {
                                            str_czsl = "00000000" + Convert.ToInt64(str_czsl).ToString("x");
                                            str_czsl = str_czsl.Substring(str_czsl.Length - 8);
                                        }
                                        else
                                        {
                                            str_Result = "参数错误";
                                            break;
                                        }

                                        string str_qydm = str[1];
                                        if (0 <= Convert.ToInt64(str_qydm) && Convert.ToInt64(str_qydm) <= 65535)
                                        {
                                            str_qydm = "0000" + Convert.ToInt64(str_qydm).ToString("x");
                                            str_qydm = str_qydm.Substring(str_qydm.Length - 4);
                                        }
                                        else
                                        {
                                            str_Result = "参数错误";
                                            break;
                                        }

                                        str_CommPam = str_czsl + str_qydm;
                                    }
                                    catch
                                    {
                                        str_Result = "参数错误";
                                        break;
                                    }
                                }
                                else
                                {
                                    str_Result = "不支持";
                                    break;
                                }

                                str_Comm = str_MeterId + "6210000006" + str_CommPam;
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = NB_sql(MeterId, Type);

                                break;
                            }

                        //蓝牙NB
                        //未开户清零指令
                        //Type：63 | 99
                        //CommPam：区域代码
                        case "99":
                            {
                                str_xyjx = "蓝牙|未开户清零[" + CommPam + "]";
                                str_xymc = "蓝牙|未开户清零";

                                if (MeterType == "4" ||
                                    MeterType == "7" ||
                                    MeterType == "10")
                                {
                                    try
                                    {
                                        string str_qydm = CommPam;
                                        if (0 <= Convert.ToInt64(str_qydm) && Convert.ToInt64(str_qydm) <= 65535)
                                        {
                                            str_qydm = "0000" + Convert.ToInt64(str_qydm).ToString("x");
                                            str_qydm = str_qydm.Substring(str_qydm.Length - 4);
                                        }
                                        else
                                        {
                                            str_Result = "参数错误";
                                            break;
                                        }

                                        str_CommPam = str_qydm;
                                    }
                                    catch
                                    {
                                        str_Result = "参数错误";
                                        break;
                                    }
                                }
                                else
                                {
                                    str_Result = "不支持";
                                    break;
                                }

                                str_Comm = str_MeterId + "6310000002" + str_CommPam;
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = NB_sql(MeterId, Type);

                                break;
                            }

                        //蓝牙NB
                        //恢复出厂指令
                        //Type：64 | 100
                        //CommPam：区域代码
                        case "100":
                            {
                                str_xyjx = "蓝牙|恢复出厂[" + CommPam + "]";
                                str_xymc = "蓝牙|恢复出厂";

                                if (MeterType == "4" ||
                                    MeterType == "7" ||
                                    MeterType == "10")
                                {
                                    try
                                    {
                                        string str_qydm = CommPam;
                                        if (0 <= Convert.ToInt64(str_qydm) && Convert.ToInt64(str_qydm) <= 65535)
                                        {
                                            str_qydm = "0000" + Convert.ToInt64(str_qydm).ToString("x");
                                            str_qydm = str_qydm.Substring(str_qydm.Length - 4);
                                        }
                                        else
                                        {
                                            str_Result = "参数错误";
                                            break;
                                        }

                                        str_CommPam = str_qydm;
                                    }
                                    catch
                                    {
                                        str_Result = "参数错误";
                                        break;
                                    }
                                }
                                else
                                {
                                    str_Result = "不支持";
                                    break;
                                }

                                str_Comm = str_MeterId + "6410000002" + str_CommPam;
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = NB_sql(MeterId, Type);

                                break;
                            }

                        //蓝牙NB
                        //参数设置指令
                        //Type：65 | 101
                        //CommPam：报警量(m³):囤积量(m³):区域代码
                        case "101":
                            {
                                str_xyjx = "蓝牙|参数设置[" + CommPam + "]";
                                str_xymc = "蓝牙|参数设置";

                                if (MeterType == "4" ||
                                    MeterType == "7" ||
                                    MeterType == "10")
                                {
                                    try
                                    {
                                        string[] str = CommPam.Split(':');

                                        string str_bjl = str[0];
                                        str_bjl = (Convert.ToDouble(str_bjl) * 10).ToString();
                                        if (0 <= Convert.ToInt64(str_bjl) && Convert.ToInt64(str_bjl) <= 65535)
                                        {
                                            str_bjl = "0000" + Convert.ToInt64(str_bjl).ToString("x");
                                            str_bjl = str_bjl.Substring(str_bjl.Length - 4);
                                        }
                                        else
                                        {
                                            str_Result = "参数错误";
                                            break;
                                        }

                                        string str_tjl = str[1];
                                        str_tjl = (Convert.ToDouble(str_tjl) * 10).ToString();
                                        if (0 <= Convert.ToInt64(str_tjl) && Convert.ToInt64(str_tjl) <= 4294967295)
                                        {
                                            str_tjl = "00000000" + Convert.ToInt64(str_tjl).ToString("x");
                                            str_tjl = str_tjl.Substring(str_tjl.Length - 8);
                                        }
                                        else
                                        {
                                            str_Result = "参数错误";
                                            break;
                                        }

                                        string str_qydm = str[2];
                                        if (0 <= Convert.ToInt64(str_qydm) && Convert.ToInt64(str_qydm) <= 65535)
                                        {
                                            str_qydm = "0000" + Convert.ToInt64(str_qydm).ToString("x");
                                            str_qydm = str_qydm.Substring(str_qydm.Length - 4);
                                        }
                                        else
                                        {
                                            str_Result = "参数错误";
                                            break;
                                        }

                                        str_CommPam = str_bjl + str_tjl + str_qydm;
                                    }
                                    catch
                                    {
                                        str_Result = "参数错误";
                                        break;
                                    }
                                }
                                else
                                {
                                    str_Result = "不支持";
                                    break;
                                }

                                str_Comm = str_MeterId + "6510000008" + str_CommPam;
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = NB_sql(MeterId, Type);

                                break;
                            }

                        //蓝牙NB
                        //信息转移指令
                        //Type：66 | 102
                        //CommPam：累计充值(m³):累计已用(m³):区域代码
                        case "102":
                            {
                                str_xyjx = "蓝牙|信息转移[" + CommPam + "]";
                                str_xymc = "蓝牙|信息转移";

                                if (MeterType == "4" ||
                                    MeterType == "7" ||
                                    MeterType == "10")
                                {
                                    try
                                    {
                                        string[] str = CommPam.Split(':');

                                        string str_ljcz = str[0];
                                        str_ljcz = (Convert.ToDouble(str_ljcz) * 10).ToString();
                                        if (0 <= Convert.ToInt64(str_ljcz) && Convert.ToInt64(str_ljcz) <= 4294967295)
                                        {
                                            str_ljcz = "00000000" + Convert.ToInt64(str_ljcz).ToString("x");
                                            str_ljcz = str_ljcz.Substring(str_ljcz.Length - 8);
                                        }
                                        else
                                        {
                                            str_Result = "参数错误";
                                            break;
                                        }

                                        string str_ljyy = str[1];
                                        str_ljyy = (Convert.ToDouble(str_ljyy) * 10).ToString();
                                        if (0 <= Convert.ToInt64(str_ljyy) && Convert.ToInt64(str_ljyy) <= 4294967295)
                                        {
                                            str_ljyy = "00000000" + Convert.ToInt64(str_ljyy).ToString("x");
                                            str_ljyy = str_ljyy.Substring(str_ljyy.Length - 8);
                                        }
                                        else
                                        {
                                            str_Result = "参数错误";
                                            break;
                                        }

                                        string str_qydm = str[2];
                                        if (0 <= Convert.ToInt64(str_qydm) && Convert.ToInt64(str_qydm) <= 65535)
                                        {
                                            str_qydm = "0000" + Convert.ToInt64(str_qydm).ToString("x");
                                            str_qydm = str_qydm.Substring(str_qydm.Length - 4);
                                        }
                                        else
                                        {
                                            str_Result = "参数错误";
                                            break;
                                        }

                                        str_CommPam = str_ljcz + str_ljyy + str_qydm;
                                    }
                                    catch
                                    {
                                        str_Result = "参数错误";
                                        break;
                                    }
                                }
                                else
                                {
                                    str_Result = "不支持";
                                    break;
                                }

                                str_Comm = str_MeterId + "661000000A" + str_CommPam;
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = NB_sql(MeterId, Type);

                                break;
                            }

                        //2020.07.07
                        //程序切换功能
                        //Type：70 | 112
                        //CommPam：00  (①、00：电信一代IOT；②、01：移动ONENET；③、10：UDP；④、11：电信AEP)
                        case "112":
                            {
                                str_xyjx = "程序切换[" + CommPam + "]";
                                str_xymc = "程序切换";

                                if (CommPam == "00" || CommPam == "01" || CommPam == "10" || CommPam == "11")
                                {
                                    str_CommPam = "00" + CommPam;
                                    str_CommPam = str_CommPam.Substring(str_CommPam.Length - 2);
                                }
                                else
                                {
                                    str_Result = "参数错误";
                                    break;
                                }

                                str_Comm = str_MeterId + "7010000001" + str_CommPam;
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = NB_sql(MeterId, Type);

                                break;
                            }

                        //2020.06.13
                        //设置数据冻结方式
                        //Type：82 | 130
                        //CommPam：00  (00：无冻结；01：零点冻结(1条)；02：整点冻结(24条))
                        case "130":
                            {
                                str_xyjx = "设置数据冻结方式[" + CommPam + "]";
                                str_xymc = "设置数据冻结方式";

                                if (CommPam == "00" || CommPam == "01" || CommPam == "02")
                                {
                                    str_CommPam = "00" + CommPam;
                                    str_CommPam = str_CommPam.Substring(str_CommPam.Length - 2);
                                }
                                else
                                {
                                    str_Result = "参数错误";
                                    break;
                                }

                                str_Comm = str_MeterId + "8210000001" + str_CommPam;
                                str_Comm = str_Comm + WebSdkCore.ToModbusCRC16(str_Comm, true);
                                str_Comm = "1B06" + str_Comm;
                                str_Comm = str_Comm.ToUpper();

                                str_Result = NB_sql(MeterId, Type);

                                break;
                            }

                     
                    }
                }
                catch
                {
                    str_Result = "仪表编号不正确";
                }
            }
            return str_Result;
        }
        
        public static string NB_sql(string MeterId, string Type)
        {
            DateTime str_dt = DateTime.Now;

            bool operatorlog = WebSdkCore.insert_update_delete("insert into rp_meter_cx(ybdz,mllx,mlnr,bcsj,zxjg) values ('" +
                                                                MeterId + "','" +
                                                                Type + "','" +
                                                                str_Comm + "','" +
                                                                str_dt + "','" +
                                                                str_zxjg + "')");

            if (operatorlog == true)
            {
                operatorlog = WebSdkCore.insert_update_delete("insert into rp_meter_rz(ybdz,xynr,xyjx,xtsj,rzlx,xymc,czy) values ('" +
                                                                    MeterId + "','" +
                                                                    str_Comm + "','" +
                                                                    str_xyjx + "','" +
                                                                    str_dt + "','" +
                                                                    str_rzlx + "','" +
                                                                    str_xymc + "','" +
                                                                    str_oper + "')");
                if (operatorlog == true)
                {
                    str_Result = "成功";
                }
                else
                {
                    str_Result = "写日志失败";
                }
            }
            else
            {
                str_Result = "写指令失败";
            }

            return str_Result;
        }
    }
}